#include <time.h>
#include "randomutil.h"

RandomUtil* RandomUtil::s_inst = 0;

RandomUtil* RandomUtil::inst()
{
    if (!s_inst) {
        s_inst = new RandomUtil();
    }
    return s_inst;
}

int RandomUtil::getRandomDate(const char *symbol)
{
    if (m_mapDate.find(symbol) != m_mapDate.end()) {
        return m_mapDate[symbol];
    }
    return 0;
}

double RandomUtil::getRandomPrice(const char *symbol)
{
    if (m_mapPrice.find(symbol) != m_mapPrice.end()) {
        return m_mapPrice[symbol];
    }
    return 0;
}

void RandomUtil::insertRandomDate(const char *symbol)
{
    if (m_mapDate.find(symbol) == m_mapDate.end()) {
        srand(time(NULL));
        m_mapDate[symbol] = (0.5 * RAND_MAX - rand()) * 365 * 24 * 10 / RAND_MAX;
        m_mapDate[symbol] *= 3600;
    }
}

void RandomUtil::insertRandomPrice(const char *symbol)
{
    if (m_mapPrice.find(symbol) == m_mapPrice.end()) {
        srand(time(NULL));
        m_mapPrice[symbol] = (0.5 * RAND_MAX - rand() ) / RAND_MAX;
    }
}

void RandomUtil::removeRandomDate(const char *symbol)
{
    std::map<std::string,int>::iterator itr = m_mapDate.find(symbol);
    if (itr != m_mapDate.end()) {
        m_mapDate.erase(itr);
    }
}

void RandomUtil::removeRandomPrice(const char *symbol)
{
    std::map<std::string,double>::iterator itr = m_mapPrice.find(symbol);
    if (itr != m_mapPrice.end()) {
        m_mapPrice.erase(itr);
    }
}